<?php
namespace Hawk\Controller;
use \Hawk\Controller\IndexController;
class SaleController extends IndexController 
{
	/**
     * 业务客户列表
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
	public function lst(){
		$model = M('');
		$mlmodel = D('Hawk/Customer');
		$rmodel = M('Role');
		$almodel = M('AdminRole');
		$amodel = D('Hawk/Admin');
		$pmodel=M('Part');
		if(IS_POST){	
			if(!empty($_POST['part_id'])){
				$pid = $_POST['part_id'];
				$search['part_id'] = $_POST['part_id'];
			}else{
				$pid = session('staff_info')['part_id'];
			}
			if(!empty($_POST['sale_worker'])){
				$sale_worker = $_POST['sale_worker'];
				$search['sale_worker'] = $_POST['sale_worker'];
			}else{
				$sale_worker = '';
			}
			if(!empty($_POST['cus_id'])){
				$customer = $_POST['cus_id'];
				$search['cus_id'] = $_POST['cus_id'];
			}else{
				$customer = '';
			}
			if(!empty($_POST['time_start'])){
				$time_start =$_POST['time_start'];
				$search['time_start']=$_POST['time_start'];
			}else{
				$time_start ='';
			}
			if(!empty($_POST['time_end'])){
				$time_end =$_POST['time_end'];
				$search['time_end']=$_POST['time_end'];
			}else{
				$time_end = '';
			}
		}
		if(empty($sale_worker)){
			$where_admin = $this->_where_sale_worker($pid);
			if(empty($where_admin['sale_worker'][1])){
				$where_admin= array('sale_worker'=>array('eq',0));
			}
		}else{
			$where_admin = array('sale_worker'=>array('0'=>'IN','1'=>array('0'=>$sale_worker)));
		}
		//查取该业务员涉及的客户ID
		$where_cus = $this->_where_customer($where_admin);
		if(empty($where_cus['id'][1])){
			$where_cus=array('id'=>array('0'=>'IN','1'=>array('0'=>0)));
		}
		if(!empty($customer)){
			$where_cus = array('id'=>array('0'=>'IN','1'=>array('0'=>$customer)));
		}
		if(!empty($time_start)&&!empty($time_end)){
			$where_cus['updatetime']=array(array('gt',strtotime($time_start)),array('lt',strtotime($time_end)+3600*24)); 
		}else if(!empty($time_start)&&empty($time_end)){
			$where_cus['updatetime']=array('gt',strtotime($time_start));
		}else if(empty($time_start)&&!empty($time_end)){
			$where_cus['updatetime']=array('lt',strtotime($time_end)+3600*24);
		}
		//查询该业务员涉及的客户列表
		$count = $model->table(C('DB_PREFIX').'customer')->where($where_cus)->count();
		$page = new \Think\Page($count, 10);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$list['page'] = $page->show();
		$list['list'] = $model
		->table(C('DB_PREFIX').'customer')
		->where($where_cus)
		->limit($page->firstRow . ',' . $page->listRows)
		->select();
		$list['count']=count($list['list']);
		if($list['count']>0){
			foreach ($list['list'] as $k => $v) {
				$list['list'][$k]['allMoney'] = array_sum($mlmodel->getCustomerMoney($v['id']));
				$list['allmoney'] .= array_sum($mlmodel->getCustomerMoney($v['id']));
				$list['list'][$k]['server_worker_name']=$amodel->getAdminName($v['server_worker']);
				$list['list'][$k]['sale_worker_name']=$amodel->getAdminName($v['sale_worker']);
				$list['list'][$k]['sale_head_name']=$amodel->getAdminName($v['sale_head']);
				$list['list'][$k]['sale_manager_name']=$amodel->getAdminName($v['sale_manager']);
			}
		}		
		//计算所有客户总订单额
		$w=$where_admin;
		$w['cus_id']=$where_cus['id'];
		$w['sale_type']='worker';
		if(!empty($where_cus['updatetime'])){
			$w['updatetime']=$where_cus['updatetime'];
		}else{
			$w['addtime']=array('gt',strtotime(date('Y-m',time())));
		}
		$list['yeji'] = $this->_yeji($w);
		//查询部门
		$part_id =$this->_part_id(session('staff_info')['part_id']);
		if(empty($part_id)){
			$list['part']=array();
		}else{
			$part_where= array('id'=>array('IN',$part_id));
			$list['part']=$this->_part_data($part_where);
		}	
		//查询涉及业务员
		$where_sale = $this->_where_sale_worker($pid);
		if(is_array($where_sale['sale_worker'])){
			if(!empty($where_sale['sale_worker'][1])){
				$sale_worker_where = array('sale_worker'=>array('IN',$where_sale['sale_worker'][1]));
			}else{
				$sale_worker_where = array('sale_worker'=>array('eq',0));
			}
		}else{
			$sale_worker_where =array('sale_work'=>array('eq',$where_sale['sale_worker'])); 
		}
		$list['sale_worker'] = $this->_sale_worker_data($sale_worker_id);
		//查询涉及客户
		//添加条件只涉及业务员
		$sale_worker_where['sale_type']='worker';
		$where_customer = $this->_where_customer($sale_worker_where);
		if(empty($where_customer['id'][1])){
			$where_customer=array('id'=>array('0'=>'IN','1'=>array('0'=>0)));
		}
		$list['customer'] = $model->table(C('DB_PREFIX').'customer')->where($where_customer)->select();
		$list['search'] = $search;
		$this->assign('list',$list);
		$tmodel = M('CustomerType');
		$type = $tmodel->select();
		$this->assign('type',$type);
	    $this->display(); 
	}
	/**
     * 个人客户管理
     * @author hawk <2271030162@qq.com>
     * @param  $pid int
     * @return array
     */
	public function selflst(){
		$model = M('');
		$mlmodel = D('Hawk/Customer');
		$rmodel = M('Role');
		$almodel = M('AdminRole');
		$amodel = D('Hawk/Admin');
		$pmodel=M('Part');
		if(IS_POST){	
			if(!empty($_POST['part_id'])){
				$pid = $_POST['part_id'];
				$search['part_id'] = $_POST['part_id'];
			}else{
				$pid = '';
			}
			if(!empty($_POST['sale_worker'])){
				$sale_worker = $_POST['sale_worker'];
				$search['sale_worker'] = $_POST['sale_worker'];
			}else{
				$sale_worker = '';
			}
			if(!empty($_POST['cus_id'])){
				$customer = $_POST['cus_id'];
				$search['cus_id'] = $_POST['cus_id'];
			}else{
				$customer = '';
			}
			if(!empty($_POST['time_start'])){
				$time_start =$_POST['time_start'];
				$search['time_start']=$_POST['time_start'];
			}else{
				$time_start ='';
			}
			if(!empty($_POST['time_end'])){
				$time_end =$_POST['time_end'];
				$search['time_end']=$_POST['time_end'];
			}else{
				$time_end = '';
			}
		}
		if(empty($sale_worker)){
			$where_admin = array('sale_worker'=>array('eq',session('staff_info')['id']));
		}else{
			$where_admin = array('sale_worker'=>array('eq',$sale_worker));
		}
		//查取该业务员涉及的客户ID
		$where_cus = $this->_where_customer($where_admin);
		if(empty($where_cus['id'][1])){
			$where_cus=array('id'=>array('0'=>'IN','1'=>array('0'=>0)));
		}
		if(!empty($customer)){
			$where_cus = array('id'=>array('0'=>'IN','1'=>array('0'=>$customer)));
		}
		if(!empty($time_start)&&!empty($time_end)){
			$where_cus['updatetime']=array(array('gt',strtotime($time_start)),array('lt',strtotime($time_end)+3600*24)); 
		}else if(!empty($time_start)&&empty($time_end)){
			$where_cus['updatetime']=array('gt',strtotime($time_start));
		}else if(empty($time_start)&&!empty($time_end)){
			$where_cus['updatetime']=array('lt',strtotime($time_end)+3600*24);
		}
		//查询该业务员涉及的客户列表
		$count = $model->table(C('DB_PREFIX').'customer')->where($where_cus)->count();
		$page = new \Think\Page($count, 10);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$list['page'] = $page->show();
		$list['list'] = $model
		->table(C('DB_PREFIX').'customer')
		->where($where_cus)
		->limit($page->firstRow . ',' . $page->listRows)
		->select();
		foreach ($list['list'] as $k => $v) {
			$list['list'][$k]['allMoney'] = array_sum($mlmodel->getCustomerMoney($v['id']));
			$list['allmoney'] .= array_sum($mlmodel->getCustomerMoney($v['id']));
			$list['list'][$k]['server_worker_name']=$amodel->getAdminName($v['server_worker']);
			$list['list'][$k]['sale_worker_name']=$amodel->getAdminName($v['sale_worker']);
			$list['list'][$k]['sale_head_name']=$amodel->getAdminName($v['sale_head']);
			$list['list'][$k]['sale_manager_name']=$amodel->getAdminName($v['sale_manager']);
		}
		$list['count']=count($list['list']);
		//计算所有客户总订单额
		$w=$where_admin;
		$w['cus_id']=$where_cus['id'];
		if(!empty($where_cus['updatetime'])){
			$w['updatetime']=$where_cus['updatetime'];
		}else{
			$w['addtime']=array('gt',strtotime(date('Y-m',time())));
		}
		$list['yeji'] = $this->_yeji($w);
		//查询部门
		$part_id =$this->_part_id(session('staff_info')['part_id']);
		if(empty($part_id)){
			$list['part']=array();
		}else{
			$part_where= array('id'=>array('IN',$part_id));
			$list['part']=$this->_part_data($part_where);
		}	
		//查询涉及业务员
		$where_sale = $this->_where_sale_worker($pid);
		if(is_array($where_sale['sale_worker'])){
			if(!empty($where_sale['sale_worker'][1])){
				$sale_worker_where = array('sale_worker'=>array('IN',$where_sale['sale_worker'][1]));
			}else{
				$sale_worker_where = array('sale_work'=>array('eq',0));
			}
		}else{
			$sale_worker_where =array('sale_work'=>array('eq',$where_sale['sale_worker'])); 
		}
		$list['sale_worker'] = $this->_sale_worker_data($sale_worker_where);
		//查询涉及客户
		$where_customer = $this->_where_customer($where_sale);
		if(empty($where_customer['id'][1])){
			$where_customer=array('id'=>array('0'=>'IN','1'=>array('0'=>0)));
		}
		$list['customer'] = $model->table(C('DB_PREFIX').'customer')->where($where_customer)->select();
		$list['search'] = $search;
		$this->assign('list',$list);
		$tmodel = M('CustomerType');
		$type = $tmodel->select();
		$this->assign('type',$type);
	    $this->display();
	}
	/**
     * 涉及业务员ID
     * @author hawk <2271030162@qq.com>
     * @param  $pid int
     * @return array
     */
	private function _where_sale_worker($pid){
		$model = M('');
		$rmodel = M('Role');
		$almodel = M('AdminRole');
		$amodel = D('Hawk/Admin');
		$pmodel=M('Part');
		//部门层级筛选
		$part_id = $this->_part_id($pid);
		//角色层级筛选
		$role_id = $this->_role_id();
		//涉及业务员条件
		if(empty($role_id)){
			//专员视窗
			$where_admin['sale_worker']=session('id');		
		}else{
			//管理人员视窗
			//所有人员
			$admin_id = $model
			->table(C('DB_PREFIX').'admin_role')
			->alias('ar')
			->field('admin_id')
			->join('left join hawk_admin a on ar.admin_id=a.id left join hawk_part p on a.part_id=p.id')
			->where(array(
				'role_id'=>array('IN',$role_id),
				'part_id'=>array('IN',$part_id)
				))
			->select();
			//重组数组(二维变一维)
			foreach ($admin_id as $k => $v) {
				$aid[]=$v['admin_id'];
			}
			$where_admin=array('sale_worker'=>array('IN',$aid));
		}
		return $where_admin;
	}
	/**
     * 部门层级筛选(返回部门ID)
     * @author hawk <2271030162@qq.com>
     * @param  $pid int
     * @return array
     */
	private function _part_id($pid){
		$amodel = D('Hawk/Admin');
		$pmodel=M('Part');
		//通过部门层级查询
		if(empty($pid)){
			$pid = session('staff_info')['part_id'];
		}
		$part_id = $amodel->getChildren($pmodel,$pid);
		array_unshift($part_id, $pid);
		return $part_id;
	}
	/**
     * 角色层级筛选(返回角色ID)
     * @author hawk <2271030162@qq.com>
     * @return array
     */
	private function _role_id(){
		$amodel = D('Hawk/Admin');
		$almodel = M('AdminRole');
		$rmodel = M('Role');
		//角色层级查询
		$role = $almodel
		->where(array(
			'admin_id'=>array('eq',session('staff_info')['id'])
			))
		->find();
		$role_id = $amodel->getChildren($rmodel,$role['role_id']);
		array_unshift($role_id, $role['role_id']);
		return $role_id;
	}
	/**
     * 查询该业务员涉及客户
     * @author hawk <2271030162@qq.com>
     * @param  $where_admin array
     * @return array
     */	
	private function _where_customer($where_admin){
		$model = M('');
		$cus_id = $model
		->table(C('DB_PREFIX').'money_divide')
		->field('cus_id')
		->where($where_admin)
		->group('cus_id')
		->select();
		//重组数组(二维变一维)
		foreach ($cus_id as $k => $v) {
			$cid[]=$v['cus_id'];
		}
		$where_cus=array('id'=>array('IN',$cid));
		return $where_cus;
	}
	/**
     * 查询部门
     * @author hawk <2271030162@qq.com>
     * @param  $where array
     * @return array
     */	
	private function _part_data($where=true){
		$model=M('');
		$part = $model
		->table(C('DB_PREFIX').'part')
		->where($where)
		->select();
		return $part;
	}
	/**
     * 查询业务员
     * @author hawk <2271030162@qq.com>
     * @param  $sale_worker_id array
     * @return array
     */	
	private function _sale_worker_data($where){
		$model=M('');
		$sale_worker = $model
		->table(C('DB_PREFIX').'admin')
		->field('id,nickname')
		->where($where)
		->select();
		return $sale_worker;
	}
	/**
     * 业务展示客户沟通记录
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
	public function chat(){
		$id=I('get.id');
		if(!empty($id)){
			//查询客户沟通记录
			$clmodel=M('CommunicateLog');
			$where = array('cus_id'=>array('eq',$id),'status'=>array('eq',0));
			$count = $clmodel->where($where)->count();
			$page = new \Think\Page($count, 1);
			// 配置翻页的样式
			$page->setConfig('prev', '上一页');
			$page->setConfig('next', '下一页');
			$cus['page'] = $page->show();
			$cus['communicate'] = $clmodel->where($where)
										->order('addtime desc')
										->limit($page->firstRow.','.$page->listRows)
										->select();
			$this->assign('cus',$cus);
			$this->display();
		}else{
			hecho('无此权限');
		}
	}
	/**
     * 业务展示客户股票记录
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
	public function stock(){
		$id=I('get.id');
		if(!empty($id)){
			$pmodel=M('StockLog');
			$cus['position'] = $pmodel->where(array('cus_id'=>array('eq',$id),'position_num'=>array('GT',0)))->select();
			//获取持仓股票现价
			if(count($cus['position']>0)){
				foreach ($cus['position'] as $k => $v) {
					if(substr($v['stock_code'], 0,1)==6){
						$code = 'sh'.$v['stock_code'];
					}else{
						$code = 'sz'.$v['stock_code'];
					}
					$url = "http://hq.sinajs.cn/list=".$code;
					$json=explode(',',substr(hawk_stock_price($url), 21,-3));
					$cus['position'][$k]['price']=$json[4];
					$stock[]=array('name'=>$json[0],
						'price'=>$json[4]);
					$market_value []= $v['position_num']*$json[4];			
				}
			}
			$this->assign('cus',$cus);
			$this->display();	
		}else{
			echo json_encode(array('code'=>1000,'msg'=>'您无此权限！'));
			
		}
	}
	/**
     * 计算业绩
     * @author hawk <2271030162@qq.com>
     * @return array
     */
	private function _yeji($where=true){
		$mlmodel = D('Hawk/Customer');
		$data = array_sum($mlmodel->getSaleMoney($where));
		if(empty($data)){
			$data='0';
		}
		return array('code'=>1,'msg'=>$data);
	}
	
	/**
     * ajax 查询业绩（未使用）
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
	public function ajaxyeji(){
		if(IS_POST){
			$model=M('Customer');
			$w=$this->_where_sale_worker();
			if(empty($where)){
				$where=true;
			}
			$w['addtime']=array(array(
				'gt',strtotime($_POST['start_time'])
				),array(
				'lt',strtotime($_POST['end_time'])+3600*24
				)) ;
			$list['yeji'] = $this->_yeji(session('id'),$w);
			echo json_encode($list['yeji']);
		}
		
	}
	// private function _yeji($data,$where=true){
	// 	$mlmodel = D('Hawk/Customer');

	// 	if(is_array($data)){
	// 		foreach ($data as $k => $v) {
	// 			$yeji[]=array_sum($mlmodel->getCustomerMoney($v['id'],$where));
	// 		}
	// 		$all= array('code'=>1,'msg'=>array_sum($yeji));
	// 	}else{
	// 		$all = array('code'=>0,'msg'=>'本月还没业绩哦，继续努力吧。');
	// 	}
	// 	return $all; 
	// }
	/**
	 *
	 *
	 */
	
}